package org.jbox2d.testbed;

import java.util.ArrayList;

import org.jbox2d.collision.AABB;
import org.jbox2d.collision.shapes.CircleDef;
import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.DebugDraw;
import org.jbox2d.dynamics.World;
import org.jbox2d.testbed.AbstractExample.BoundImage;

import processing.core.PApplet;

public class MySelfExample extends PApplet
{
	protected DebugDraw m_debugDraw;
	protected World m_world;
	protected AABB m_worldAABB;
	
	protected ArrayList<BoundImage> boundImages = new ArrayList<BoundImage>();
	
	@Override
	public void setup()
	{
		size(900, 600, P3D);
		frameRate(60.f);
		m_debugDraw=new ProcessingDebugDraw(this);
		m_worldAABB=new AABB();
		m_worldAABB.lowerBound.set(new Vec2(-200,-50));
		m_worldAABB.upperBound.set(new Vec2(200,50));
		m_world=new World(m_worldAABB, new Vec2(0f,-9.8f), true);
				
		m_world.setDebugDraw(m_debugDraw);
		
		m_debugDraw.setCamera(0.0f, 0.0f, 8.0f);
		
		/*创建物体*/
		PolygonDef groundPolygonDef = new PolygonDef();
		groundPolygonDef.setAsBox(50.0f, 10.0f);

		BodyDef groundBodyDef = new BodyDef();
		groundBodyDef.position = new Vec2(0.0f, 0f);
		m_world.createBody(groundBodyDef).createShape(groundPolygonDef);
		
		CircleDef testCircleDef=new CircleDef();
		testCircleDef.density=1.0f;
		testCircleDef.friction=1.0f;
		testCircleDef.radius=1.0f;
		testCircleDef.restitution=1.0f;
		
		BodyDef testCircleBodyDef=new BodyDef();
		testCircleBodyDef.allowSleep=true;
		testCircleBodyDef.position=new Vec2(-20.0f, 20.0f);
		
		Body testCircleBody=m_world.createBody(testCircleBodyDef);
		testCircleBody.createShape(testCircleDef);
		testCircleBody.setMassFromShapes();
		testCircleBody.setLinearVelocity(new Vec2(10.0f, 0.0f));
		/*创建物体*/
	}
	
	@Override
	public void draw()
	{
		final float timeStep=1/60f;
		final int iterationCount=10;
		background(0);
		m_debugDraw.setFlags(0);
		m_debugDraw.appendFlags(DebugDraw.e_shapeBit);
		m_debugDraw.appendFlags(DebugDraw.e_jointBit);
		m_debugDraw.appendFlags(DebugDraw.e_coreShapeBit);
		m_debugDraw.appendFlags(DebugDraw.e_aabbBit);
		m_debugDraw.appendFlags(DebugDraw.e_obbBit);
		m_debugDraw.appendFlags(DebugDraw.e_pairBit);
		m_debugDraw.appendFlags(DebugDraw.e_centerOfMassBit);
		m_debugDraw.appendFlags(DebugDraw.e_controllerBit);
		
		m_world.setWarmStarting(true);
		m_world.setPositionCorrection(true);
		m_world.setContinuousPhysics(true);
		
		m_world.step(timeStep, iterationCount);
		
		for (BoundImage b:boundImages)
		{
            b.draw();
        }
	}
	
	public static void main(String[] args)
	{
		PApplet.main(new String[] { "org.jbox2d.testbed.MySelfExample" });
	}
}
